﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace calculator
{
    class ExpressionString
    {
        private string expression;
        private char[] varList;
        private List<string> expressionList = new List<string>();

        public ExpressionString(string expression)
        {
            this.expression = expression;
            stringToList();

            fillVariableList();
        }
        public char[] getVarList()
        {
            return varList;
        }
        public List<string> getExpressionList()
        {
            return expressionList;
        }
        private void stringToList()
        {
            //raðar expression upp eftir bilum
            int counter = 0;
            for (int i = 0; i < expression.Length; i++)
            {
                if (expression[i] == '(' || expression[i] == ')')
                {
                    expressionList.Add(expression.Substring(counter, i - counter));
                    expressionList.Add(expression[i].ToString());
                    counter = i+1;
                }
                
                if (expression[i] == ' ')
                {
                    expressionList.Add(expression.Substring(counter, i - counter));
                    counter = i + 1;
                }
                if (i == expression.Length - 1)
                {
                    expressionList.Add(expression.Substring(counter, i - counter+1));
                }
            }
            //Nýtt
            while (expressionList.IndexOf("") != -1)
                expressionList.Remove("");
        }
        private void fillVariableList()
        {
            //tekur alla characters út sem standa einir í listanum og gerir þá að breytum
           
            List<string> tmpList = new List<string>();

            for (int i = 0; i < expressionList.Count; i++ )
            {
                if (expressionList[i].Length == 1 && tmpList.IndexOf(expressionList[i]) == -1 && expressionList[i] != "(" && expressionList[i] != ")")
                {
                    tmpList.Add(expressionList[i]);
                }
            }
            varList = new char[tmpList.Count];
            for (int i = 0; i < tmpList.Count; i++)
            {
                char tmpChar;
                if (char.TryParse(tmpList[i].ToString(), out tmpChar))
                    varList[i] = tmpChar; 
            }
            
            
        }
    }
}
